package net.ojbk.study.curriculumDesign.web.handlers.cilentservicer.impl;

import net.ojbk.study.curriculumDesign.beans.cilentservicer.Chat;
import net.ojbk.study.curriculumDesign.beans.cilentservicer.ChatMessage;
import net.ojbk.study.curriculumDesign.beans.cilentservicer.ChatSystem;
import net.ojbk.study.curriculumDesign.beans.cilentservicer.Servicer;
import net.ojbk.study.curriculumDesign.beans.usermanage.User;
import net.ojbk.study.curriculumDesign.service.cilentservicer.IServicerCommService;
import net.ojbk.study.curriculumDesign.service.cilentservicer.IServicerManaService;
import net.ojbk.study.curriculumDesign.web.handlers.cilentservicer.IServicerController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: twobox
 * Date: 2019/1/9
 * Time: 22:03
 * Description: No Description
 */
@Controller
@RequestMapping("cilentservicer")
public class ServicerControllerImpl implements IServicerController {
    @Autowired
    @Qualifier("chatSystem")
    ChatSystem chatSystem;

    @Autowired
    @Qualifier("servicerCommService")
    IServicerCommService servicerComService;

    @Autowired
    @Qualifier("servicerManaService")
    IServicerManaService servicerManaService;

    /**
     * 获取服务list serviceUserList
     *
     * @param session
     * @return
     */
    @RequestMapping(value = "serviceUserList", method = RequestMethod.GET)
    public ResponseEntity<Object> getAllServiceUserList(HttpSession session) {
        Servicer servicer = (Servicer) session.getAttribute("servicer");
        try {
            if (servicer == null)
                throw new Exception("你不是客服！或从客服登陆！");
            List<Chat> chatList = servicer.getsChatList();
            if (chatList == null)
                throw new Exception("客服服务列表存在问题，建议重新登陆");

            List<String> uidList = new ArrayList<>();
            for (Chat chat : chatList)
                uidList.add(chat.getCuser().getUid());

            return new ResponseEntity<Object>(uidList, HttpStatus.OK);
        } catch (Exception e) {
            return new ResponseEntity<Object>(e.getMessage(), HttpStatus.BAD_REQUEST);
        }

    }

    // TODO 以上为未定义接口的

    /**
     * 查看是否有新消息
     *
     * @param httpSession
     * @return
     */
    @Override
    @RequestMapping(value = "serCall", method = RequestMethod.GET)
    public ResponseEntity<Object> isHaveNewMsg(HttpSession httpSession) {
        Servicer servicer = (Servicer) httpSession.getAttribute("servicer");
        try {
            return new ResponseEntity<Object>(servicerManaService.isHaveNewMsg(servicer), HttpStatus.OK);
        } catch (Exception e) {
            return new ResponseEntity<Object>(e.getMessage(), HttpStatus.OK);
        }
    }

    /**
     * 设置客服状态
     *
     * @param httpSession
     * @param newStatus   新状态代码
     * @return
     */
    @Override
    public ResponseEntity<Object> setMeStatus(HttpSession httpSession, int newStatus) {
        return null;
    }

    /**
     * 获取自己的评分
     *
     * @param httpSession
     * @return 分数
     */
    @Override
    public ResponseEntity<Object> getMeScore(HttpSession httpSession) {
        return null;
    }

    /**
     * 连接一个客户
     *
     * @param httpSession
     * @param user        客户的编号
     * @return
     */
    @Override
    @RequestMapping(value = "joinChat", method = RequestMethod.POST)
    public ResponseEntity<Object> joinChat(HttpSession httpSession, @RequestBody User user) {

        Servicer servicer = (Servicer) httpSession.getAttribute("servicer");

        try {
            Chat chat = chatSystem.getBeginChatByUser(user);
            servicerComService.joinChat(servicer, chat);

            return new ResponseEntity<Object>("加入会话成功", HttpStatus.OK);
        } catch (Exception e) {
            return new ResponseEntity<Object>(e.getMessage(), HttpStatus.BAD_REQUEST);
        }
    }

    /**
     * 获取所有与该用户的一条新消息
     *
     * @param httpSession
     * @param uid
     * @return
     */
    @Override
    @RequestMapping(value = "serNewMsg", method = RequestMethod.GET)
    public ResponseEntity<Object> getnewMsg(HttpSession httpSession, String uid) {
        try {
            Chat chat = chatSystem.getChatingByUser(new User(uid));
            ChatMessage newMsg = servicerComService.getNewMsg(chat);
            if (newMsg == null)
                throw new Exception("空");
            return new ResponseEntity<Object>(newMsg, HttpStatus.OK);
        } catch (Exception e) {
            return new ResponseEntity<Object>(e.getMessage(), HttpStatus.NOT_FOUND);
        }
    }

    @RequestMapping(value = "sermsgList", method = RequestMethod.GET)
    public ResponseEntity<Object> getMsgList(HttpSession session, String uid) {
        try {
            Chat chat = chatSystem.getChatingByUser(new User(uid));
            List<ChatMessage> msgList = chat.getMsgList();
            chat.getSuserNewMsgList().clear();
            return new ResponseEntity<Object>(msgList, HttpStatus.OK);
        } catch (Exception e) {
            return new ResponseEntity<Object>(e.getMessage(), HttpStatus.BAD_REQUEST);
        }
    }

    /**
     * 发送一条新消息
     *
     * @param httpSession
     * @param chatMessage 新的消息
     * @return
     */
    @Override
    @RequestMapping(value = "serMsg", method = RequestMethod.POST)
    public ResponseEntity<Object> sendNewMsg(HttpSession httpSession, String uid, @RequestBody ChatMessage chatMessage) {
        System.out.println("Uid:" + uid);
        Servicer servicer = (Servicer) httpSession.getAttribute("servicer");
        try {
            Chat chat = chatSystem.getChatingByUser(new User(uid));
            servicerComService.sendMsg(servicer, chat, chatMessage);
            return new ResponseEntity<Object>(HttpStatus.OK);
        } catch (Exception e) {
            return new ResponseEntity<Object>("error.", HttpStatus.OK);
        }
    }

    /**
     * 断开连接
     *
     * @param httpSession
     * @param uid         被断开的用户id
     * @return
     */
    @Override
    public ResponseEntity<Object> catChat(HttpSession httpSession, String uid) {
        return null;
    }

    /**
     * 获取客服的信息
     *
     * @param session
     * @return Servicer对象 但是不包括最后一个最后一个属性(聊天记录)
     */
    @Override
    @RequestMapping(value = "servicerInfo", method = RequestMethod.GET)
    public ResponseEntity<Object> getServiceInfo(HttpSession session) {
        User user = (User) session.getAttribute("user");
        try {
            Servicer servicer = chatSystem.findMe(user);
            if (session.getAttribute("servicer") == null) {
                session.setAttribute("servicer", servicer);
            }
            Servicer tempServicer1 = new Servicer(servicer.getUid(), servicer.getName(), servicer.getStatus(), servicer.getDivScore());
            return new ResponseEntity<Object>(tempServicer1, HttpStatus.OK);
        } catch (Exception e) {
            return new ResponseEntity<Object>(e.getMessage(), HttpStatus.BAD_REQUEST);
        }

    }
}
